/******************************************************************************
 * Copyright (C) 2024 EternalChip, Inc.(Gmbh) or its affiliates.
 * 
 * All Rights Reserved.
 * 
 * @file bsp_key.c
 * 
 * @par dependencies 
 * - bsp_key.h
 * - stdio.h
 * - stdint.h
 * 
 * @author Zhang Yuying
 * 
 * @brief Provide the HAL APIs of Key and corresponding opetions.
 * 
 * Processing flow:
 * 
 * call directly.
 * 
 * @version V1.0 2025-08-08
 *
 * @note 1 tab == 4 spaces!
 * 
 *****************************************************************************/

#include "bsp_key.h"

//******************************** Defines **********************************//
//******************************** Thread Func **********************************//
osThreadId_t key_TaskHandle;
const osThreadAttr_t key_Task_attributes = {
  .name = "key_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow7,
};
//******************************** Thread Func **********************************//

/************************* Queue Handler **************************************/
QueueHandle_t key_queue;
/************************* Queue Handler **************************************/
//******************************** Defines **********************************//

key_status_t key_scan(key_press_status_t *key_value)
{
    uint32_t counter = 0;
    key_press_status_t key_status_value = KEY_NOT_PRESSED;

    while (counter < 1000)
    {
        /* 如果按键按下，则发送对应的消息队列 */
        if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
        {
            key_status_value = KEY_PRESSED;
            *key_value = key_status_value;
            return KEY_OK;
        }    

        /* 按键没有被按下 */
        counter++;
    }
    *key_value = key_status_value;
    
    /* 一直没有被按下，则返回超时 */
    return KEY_ERRORTIMEOUT;
}

/**
  * @brief  Function implementing the keyTask thread.
  * @param  argument: Not used
  * @retval None
  */
void key_task_func(void *argument)
{
    key_status_t key_ret          =          KEY_OK;
    key_press_status_t key_status = KEY_NOT_PRESSED;
    key_queue = xQueueCreate(10, sizeof(key_status_t));
    osDelay(100);
    if (NULL == key_queue) {
        printf("Queue Create Failed\r\n");
    } else {
        printf("Queue Create Success\r\n");
    }
    for(;;)
    {
        //printf("Hello World\r\n");
        if (NULL != key_queue)
        {
            key_ret = key_scan(&key_status);

            if (KEY_OK == key_ret) 
            {
                if (KEY_PRESSED == key_status) 
                {
                    printf("KEY PRESSED\r\n");
                    if (pdPASS != xQueueSendToFront(key_queue, &key_status, 0)) 
                    {
                        //printf("Queue Send Failed\r\n");
                    } 
                    else 
                    {
                        printf("Queue Send Success\r\n");
                    }
                }
            }

            if (KEY_OK != key_ret) {
                //printf("KEY NOT PRESSED\r\n");
            }


        }
    }
}



